package com.ghsc.ghxc.jsb.ui.message.activity

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.event.RxBus
import com.cn.appcore.event.RxBusBean
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.cn.appcore.widget.CustomRefreshListener
import com.cn.appcore.widget.RefreshHelper
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.databinding.ActivityGroupApplysBinding
import com.ghsc.ghxc.jsb.ui.message.bean.ChatDataResultBean
import com.ghsc.ghxc.jsb.view.NotDataView
import com.scwang.smart.refresh.layout.api.RefreshLayout

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.message.activity
 * 类名：GroupApplyListActivity
 * 创建时间：2024/5/9 18:09
 * 创建人 @author： xiaolin
 * 描述 ：Todo...群申请人
 */
class GroupApplyListActivity  : BaseVMActivity<GroupApplyListViewModel, ActivityGroupApplysBinding>(){
    override fun attachLayoutRes(): Int {
        return R.layout.activity_group_applys
    }

    override fun attachVMClass(): Class<GroupApplyListViewModel>? {
        return GroupApplyListViewModel::class.java
    }

    private val mAdapter: SearchChatAdapter by lazy { SearchChatAdapter() }
    private var notDataView: View? = null
    var mList = arrayListOf<ChatDataResultBean>()
    var itemPosition =-1


    var bundle : Bundle? =null
    var gid:String  =""
    var groupPosition :Int =0

    var applycnt:Int =0 //当前申请的人数量

    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }
    override fun initView() {
        bundle=intent.extras
        gid=bundle!!.getString("gid","")
        groupPosition=bundle!!.getInt("groupPosition",0)
        binding.run {
            bar.topTitle.text="申请群聊列表"
            bar.topLeftView.setSingleClickListener { finish() }
        }
        binding.rvList.layoutManager = LinearLayoutManager(this)
        binding.rvList.adapter = mAdapter
        mAdapter.setNewInstance(mList)
        notDataView = NotDataView.notDataView(this, binding.rvList, binding.refreshLayout,"暂无数据")
        mAdapter.setOnItemClickListener { adapter, view, position ->
            var  bundle = Bundle()
            bundle.putString("userIdTa",mAdapter.getItem(position).uid)
            startActivity(ChatSetSingleActivity().javaClass,bundle)
        }
        mAdapter.addChildClickViewIds(R.id.tvBtnNo,R.id.tvBtnYes)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            //跳转申请群的列表
            itemPosition=position
            when(view.id){
                R.id.tvBtnNo ->{
                    val map: HashMap<String, Any> = HashMap()
                    map["fuid"] = mAdapter.getItem(position).uid
                    map["ptype"] = 2
                    map["gid"]= gid
                    GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onMyGroupApplyMethod(it1) }
                }
                R.id.tvBtnYes ->{
                    val map: HashMap<String, Any> = HashMap()
                    map["fuid"] = mAdapter.getItem(position).uid
                    map["ptype"] = 1
                    map["gid"]= gid
                    GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onMyGroupApplyMethod(it1) }
                }
            }

        }
        initRefreshLayout()
        pageNo = 1
        getListData()
    }
    private fun initRefreshLayout() {
        RefreshHelper.initRefreshLayout(binding.refreshLayout, object : CustomRefreshListener {
            override fun _onRefresh(refreshLayout: RefreshLayout?) {
                pageNo = 1
                getListData()
            }
            override fun _onLoadMore(refreshLayout: RefreshLayout?) {
                pageNo++
                getListData()
            }
        })
    }
    private fun getListData() {
        val map: HashMap<String, Any> = HashMap()
        map["pindex"] = pageNo
        map["psize"] = pageSize
        map["gid"]= gid
        GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onMyGroupApplyListMethod(it1) }
    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@GroupApplyListActivity) {
                binding.refreshLayout.finishRefresh()
                showToast( it)
            }
            againLoginMsg.observe(this@GroupApplyListActivity) {
                binding.refreshLayout.finishRefresh()
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }

            onMyGroupApplyData.observe(this@GroupApplyListActivity){
                //同意加好友
                showToast("操作成功")
                if (itemPosition>-1){
                    mAdapter.removeAt(itemPosition)
                }
                mAdapter.notifyItemChanged(itemPosition)
                if (mAdapter.data.isNullOrEmpty()){
                    mAdapter.setEmptyView(notDataView!!)
                }
                if (applycnt>0)
                    applycnt--
                RxBus.getDefault().post(RxBusBean(CustKey.DATA_COUNT_ITEM,  CustKey.DATA_COUNT_ITEM,groupPosition,applycnt))

            }
            onMyGroupApplyListData.observe(this@GroupApplyListActivity, Observer {
                //我的群聊 列表
                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
//                applycnt=it.data.data.applycnt
//                RxBus.getDefault().post(RxBusBean(CustKey.DATA_COUNT_ITEM,  CustKey.DATA_COUNT_ITEM,groupPosition,it.data.data.applycnt))
                if (pageNo == 1) {
                    if (it.data.list.isNullOrEmpty()){
                        mAdapter.replaceData( it.data.list)
                        mAdapter.setEmptyView(notDataView!!)
                    } else {
                        mAdapter.replaceData( it.data.list)
                    }
                } else {
                    if (!it.data.list.isNullOrEmpty())
                        mAdapter.addData(it.data.list)
                }
            })
        }
    }
    inner class SearchChatAdapter   : BaseQuickAdapter<ChatDataResultBean, BaseViewHolder>(
        R.layout.apply_list_item) {
        override fun convert(helper: BaseViewHolder, item: ChatDataResultBean) {
            GlideUtil.ImageHeadLoad(this.context,item.headimg,helper.getView(R.id.userImgUrl))
            helper.setText(R.id.tvName,item.nickname)
//            if (item.level==0)
//                helper.setGone(R.id.levelBg,true)
//            else {
//                helper.setImageResource(R.id.levelBg, CharRongYunUtils.setImageLevelId(item.level))
//                    .setGone(R.id.levelBg,false)
//            }

        }
    }
}